<?php
include_once("../inc/conn.php"); 
include_once("../inc/escape.php"); 
include_once("../inc/get_joke_url.php");
include_once("../inc/get_attitude.php");
include_once("comment_page.php");

function get_joke_old()
{
	$joke_url=$_POST["url"];
	//$joke_url=substr($joke_url,1,strlen($joke_url));
	
	$sql="select * from joke where url='".$joke_url."'";
	
	$row=mysql_fetch_array(mysql_query($sql));
	
	$tagresult = mysql_query("select * from joke_tag where joke_id='".$row["id"]."'");
	$tagstr="";
	while($tagrow = mysql_fetch_array($tagresult))
	{
		$tagstr.=$tagrow["tag_name"].",";
	}
	$tagstr=substr($tagstr,0,strlen($tagstr)-1);
	
	$next_joke_url=get_joke_url($joke_url);
	
	$re_str=$next_joke_url[0]."|".$row["author"]."|".$row["pubtime"]."|".$tagstr."|".$row["title"]."|".$row["content"]."|".$row["id"]."|".comment_page(1,$row["id"])."|".get_attitude($row["id"]);
	//print_r($next_joke_url);
	echo escape($re_str);
}

function get_joke_new()
{
	$joke_url_list= get_joke_url($joke_url, $_GET["num"]);
	$joke_list =array();
	foreach($joke_url_list as $joke_url)
	{
		$sql="select * from joke where url='".$joke_url."'";
	
		$row=mysql_fetch_array(mysql_query($sql));
		
		$tagresult = mysql_query("select * from joke_tag where joke_id='".$row["id"]."'");
		$tagstr="";
		while($tagrow = mysql_fetch_array($tagresult))
		{
			$tagstr.=$tagrow["tag_name"].",";
		}
		$tagstr=substr($tagstr,0,strlen($tagstr)-1);
				
		//$re_str=$next_joke_url[0]."|".$row["author"]."|".$row["pubtime"]."|".$tagstr."|".$row["title"]."|"."<pre>".$row["content"]."</pre>"."|".$row["id"]."|".comment_page(1,$row["id"])."|".get_attitude($row["id"]);
		//print_r($next_joke_url);
		$joke_list[] = array("author"=>escape($row["author"]), "pubtime"=>$row["pubtime"], "tagstr"=>escape($tagstr),
							"title"=>escape($row["title"]), "content"=>escape("<pre>".$row["content"]."</pre>"), 
							"id"=>$row["id"], "attitude"=>get_attitude($row["id"]), "url"=>$joke_url);	
		//echo escape($re_str);
		
	}
	//print_r($joke_list);
	echo json_encode($joke_list);
	
}

if(isset($_GET["num"]))
{
	get_joke_new();
}
else
{
	get_joke_old();
}
?>
